package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Dish;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.commons.R;
import com.itheima.dao.SetmealDishDao;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @date 2023/1/10
 */
@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
        //1.调用service的新增套餐方法
        setmealService.addSetmealWithDish(setmealDto);
        //2.返回结果
        return R.success("新增成功！");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    String name) {
        //1.调用service的分页查询
        Page<SetmealDto> setmealDtoPage = setmealService.findPage(page, pageSize, name);

        //2.返回分页查询结果
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteSetmeal(@RequestParam Long[] ids) {
        log.info("ids = ", ids.toString());
        //1.调用service的删除方法
        setmealService.deleteWithSetmealDish(ids);

        //2.返回结果
        return R.success("删除成功！");
    }

    /**
     * 查询套餐列表
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //1.根据分类id以及套餐状态进行查询，需要构造查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        //2.调用service的查询列表方法
        List<Setmeal> setmealList = setmealService.list(queryWrapper);

        //3.返回结果
        return R.success(setmealList);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealWithDishs(@PathVariable String id) {
        SetmealDto setmealDto = setmealService.getSetmealWithDishs(id);
        return R.success(setmealDto);
    }
    
    
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        boolean result = setmealService.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);
        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        
        setmealDishService.saveBatch(setmealDishes);
        
        return result ? R.success("") : R.error("更新失败");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam List<Long> ids,@PathVariable Integer status){
        LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper();
        luw.in(Setmeal::getId,ids);
        luw.set(Setmeal::getStatus,status);
        return setmealService.update(luw) ? R.success("") : R.error("状态更新失败");
    }
}
